import Mock from 'mockjs'
import { initMockJS } from './config'

// 初始化 MockJS
initMockJS()

console.log('MockJS 开始配置具体接口拦截器...')

// 用户相关接口
Mock.mock('/api/user/login', 'post', {
  code: 200,
  message: '登录成功',
  data: {
    token: '@guid',
    userInfo: {
      id: '@id',
      nickname: '@cname',
      phone: /^1[3-9]\d{9}$/,
      avatar: '@image("80x80", "#4ECDC4", "#FFF", "png", "头像")',
      email: '@email',
      gender: '@pick(["男", "女"])',
      birthday: '@date',
      address: '@region'
    }
  }
})

Mock.mock('/api/user/register', 'post', {
  code: 200,
  message: '注册成功',
  data: {
    token: '@guid',
    userInfo: {
      id: '@id',
      nickname: '@cname',
      phone: /^1[3-9]\d{9}$/,
      avatar: '@image("80x80", "#4ECDC4", "#FFF", "png", "头像")',
      email: '@email'
    }
  }
})

Mock.mock('/api/user/info', 'get', {
  code: 200,
  message: '获取用户信息成功',
  data: {
    id: '@id',
    nickname: '@cname',
    phone: /^1[3-9]\d{9}$/,
    avatar: '@image("80x80", "#4ECDC4", "#FFF", "png", "头像")',
    email: '@email',
    gender: '@pick(["男", "女"])',
    birthday: '@date',
    address: '@region',
    balance: '@float(0, 10000, 2, 2)'
  }
})

// 商品相关接口
Mock.mock('/api/products', 'get', {
  code: 200,
  message: '获取商品列表成功',
  data: {
    'list|20': [{
      id: '@id',
      name: '@ctitle(5, 15)',
      price: '@float(10, 999, 2, 2)',
      originalPrice: '@float(1000, 1999, 2, 2)',
      image: '@image("300x300", "#FF6B6B", "#FFF", "png", "商品")',
      description: '@cparagraph(1, 3)',
      sales: '@integer(0, 9999)',
      stock: '@integer(10, 999)',
      category: '@pick(["数码产品", "服装鞋帽", "家居用品", "美妆护肤", "食品饮料"])',
      rating: '@float(3, 5, 1, 1)',
      tags: '@pick(["热销", "新品", "包邮", "促销"])'
    }],
    total: 20,
    page: 1,
    pageSize: 20
  }
})

Mock.mock(/\/api\/products\/\d+/, 'get', {
  code: 200,
  message: '获取商品详情成功',
  data: {
    id: '@id',
    name: '@ctitle(5, 15)',
    price: '@float(10, 999, 2, 2)',
    originalPrice: '@float(1000, 1999, 2, 2)',
    'images|3-5': ['@image("600x600", "#FF6B6B", "#FFF", "png", "商品")'],
    description: '@cparagraph(3, 8)',
    sales: '@integer(0, 9999)',
    stock: '@integer(10, 999)',
    category: '@pick(["数码产品", "服装鞋帽", "家居用品", "美妆护肤", "食品饮料"])',
    rating: '@float(3, 5, 1, 1)',
    'tags|2-4': ['@pick(["热销", "新品", "包邮", "促销", "限时", "精选"])'],
    'specifications|3-6': [{
      name: '@ctitle(2, 4)',
      value: '@ctitle(2, 8)'
    }],
    'reviews|5-10': [{
      id: '@id',
      user: '@cname',
      avatar: '@image("40x40", "#4ECDC4", "#FFF", "png", "用户")',
      rating: '@integer(3, 5)',
      content: '@cparagraph(1, 2)',
      images: '@pick([[], ["@image(200x200)"], ["@image(200x200)", "@image(200x200)"]])',
      createTime: '@datetime'
    }]
  }
})

// 分类相关接口
Mock.mock('/api/categories', 'get', {
  code: 200,
  message: '获取分类列表成功',
  data: [
    {
      id: 1,
      name: '数码产品',
      icon: '@image("60x60", "#FF6B6B", "#FFF", "png", "数码")',
      'children|5-8': [{
        id: '@id',
        name: '@ctitle(2, 6)',
        image: '@image("120x120", "#4ECDC4", "#FFF", "png", "分类")'
      }]
    },
    {
      id: 2,
      name: '服装鞋帽',
      icon: '@image("60x60", "#4ECDC4", "#FFF", "png", "服装")',
      'children|6-10': [{
        id: '@id',
        name: '@ctitle(2, 6)',
        image: '@image("120x120", "#45B7D1", "#FFF", "png", "分类")'
      }]
    },
    {
      id: 3,
      name: '家居用品',
      icon: '@image("60x60", "#45B7D1", "#FFF", "png", "家居")',
      'children|4-7': [{
        id: '@id',
        name: '@ctitle(2, 6)',
        image: '@image("120x120", "#96CEB4", "#FFF", "png", "分类")'
      }]
    },
    {
      id: 4,
      name: '美妆护肤',
      icon: '@image("60x60", "#96CEB4", "#FFF", "png", "美妆")',
      'children|5-8': [{
        id: '@id',
        name: '@ctitle(2, 6)',
        image: '@image("120x120", "#FFEAA7", "#FFF", "png", "分类")'
      }]
    },
    {
      id: 5,
      name: '食品饮料',
      icon: '@image("60x60", "#FFEAA7", "#FFF", "png", "食品")',
      'children|6-9': [{
        id: '@id',
        name: '@ctitle(2, 6)',
        image: '@image("120x120", "#DDA0DD", "#FFF", "png", "分类")'
      }]
    }
  ]
})

// 购物车相关接口
Mock.mock('/api/cart', 'get', {
  code: 200,
  message: '获取购物车成功',
  data: {
    'items|0-5': [{
      id: '@id',
      productId: '@id',
      productName: '@ctitle(5, 15)',
      productImage: '@image("120x120", "#FF6B6B", "#FFF", "png", "商品")',
      price: '@float(10, 999, 2, 2)',
      quantity: '@integer(1, 5)',
      selected: '@boolean',
      specification: '@ctitle(2, 8)'
    }],
    totalAmount: '@float(100, 5000, 2, 2)',
    selectedCount: '@integer(0, 5)'
  }
})

Mock.mock('/api/cart/add', 'post', {
  code: 200,
  message: '添加到购物车成功',
  data: null
})

Mock.mock('/api/cart/update', 'put', {
  code: 200,
  message: '更新购物车成功',
  data: null
})

Mock.mock('/api/cart/remove', 'delete', {
  code: 200,
  message: '删除购物车商品成功',
  data: null
})

// 订单相关接口
Mock.mock('/api/orders', 'get', {
  code: 200,
  message: '获取订单列表成功',
  data: {
    'list|5-10': [{
      id: '@id',
      orderNo: /^[A-Z0-9]{16}$/,
      status: '@pick(["pending", "processing", "shipped", "completed", "cancelled"])',
      statusText: '@pick(["待付款", "待发货", "待收货", "已完成", "已取消"])',
      totalAmount: '@float(50, 2000, 2, 2)',
      createTime: '@datetime',
      'items|1-3': [{
        id: '@id',
        productName: '@ctitle(5, 15)',
        productImage: '@image("80x80", "#FF6B6B", "#FFF", "png", "商品")',
        price: '@float(10, 999, 2, 2)',
        quantity: '@integer(1, 3)',
        specification: '@ctitle(2, 8)'
      }]
    }],
    total: '@integer(5, 50)',
    page: 1,
    pageSize: 10
  }
})

Mock.mock(/\/api\/orders\/\d+/, 'get', {
  code: 200,
  message: '获取订单详情成功',
  data: {
    id: '@id',
    orderNo: /^[A-Z0-9]{16}$/,
    status: '@pick(["pending", "processing", "shipped", "completed", "cancelled"])',
    statusText: '@pick(["待付款", "待发货", "待收货", "已完成", "已取消"])',
    totalAmount: '@float(50, 2000, 2, 2)',
    createTime: '@datetime',
    payTime: '@datetime',
    shipTime: '@datetime',
    completeTime: '@datetime',
    address: {
      name: '@cname',
      phone: /^1[3-9]\d{9}$/,
      address: '@region @ctitle(5, 15)'
    },
    'items|1-3': [{
      id: '@id',
      productId: '@id',
      productName: '@ctitle(5, 15)',
      productImage: '@image("80x80", "#FF6B6B", "#FFF", "png", "商品")',
      price: '@float(10, 999, 2, 2)',
      quantity: '@integer(1, 3)',
      specification: '@ctitle(2, 8)'
    }],
    logistics: {
      company: '@pick(["顺丰速运", "中通快递", "圆通速递", "申通快递", "韵达快递"])',
      trackingNo: /^[A-Z0-9]{12}$/,
      'tracks|3-8': [{
        time: '@datetime',
        description: '@ctitle(10, 30)'
      }]
    }
  }
})

Mock.mock('/api/orders/create', 'post', {
  code: 200,
  message: '创建订单成功',
  data: {
    orderId: '@id',
    orderNo: /^[A-Z0-9]{16}$/,
    totalAmount: '@float(50, 2000, 2, 2)',
    payUrl: 'https://pay.example.com/pay/@id'
  }
})

// 搜索相关接口
Mock.mock(/\/api\/search/, 'get', {
  code: 200,
  message: '搜索成功',
  data: {
    'list|5-15': [{
      id: '@id',
      name: '@ctitle(5, 15)',
      price: '@float(10, 999, 2, 2)',
      originalPrice: '@float(1000, 1999, 2, 2)',
      image: '@image("300x300", "#FF6B6B", "#FFF", "png", "商品")',
      sales: '@integer(0, 9999)',
      rating: '@float(3, 5, 1, 1)'
    }],
    total: '@integer(5, 100)',
    page: 1,
    pageSize: 20
  }
})

// 热门搜索关键词
Mock.mock('/api/search/keywords', 'get', {
  code: 200,
  message: '获取热门搜索关键词成功',
  data: [
    '手机', '笔记本电脑', '耳机', '充电器', '数据线',
    '运动鞋', 'T恤', '牛仔裤', '连衣裙', '包包',
    '化妆品', '护肤品', '口红', '面膜', '洗面奶'
  ]
})

// 轮播图
Mock.mock('/api/banners', 'get', {
  code: 200,
  message: '获取轮播图成功',
  data: [
    {
      id: 1,
      title: '新春特惠',
      image: '@image("750x300", "#FF6B6B", "#FFF", "png", "新春特惠")',
      link: '/category/1'
    },
    {
      id: 2,
      title: '数码专场',
      image: '@image("750x300", "#4ECDC4", "#FFF", "png", "数码专场")',
      link: '/category/2'
    },
    {
      id: 3,
      title: '服装大促',
      image: '@image("750x300", "#45B7D1", "#FFF", "png", "服装大促")',
      link: '/category/3'
    }
  ]
})

// 验证所有接口是否被正确拦截
const mockEndpoints = [
  '/api/user/login',
  '/api/user/register', 
  '/api/user/info',
  '/api/products',
  '/api/categories',
  '/api/cart',
  '/api/cart/add',
  '/api/cart/update',
  '/api/cart/remove',
  '/api/orders',
  '/api/orders/create',
  '/api/search',
  '/api/search/keywords',
  '/api/banners'
]

console.log('MockJS 已启用，以下接口已被拦截:')
mockEndpoints.forEach(endpoint => {
  console.log(`✅ ${endpoint}`)
})

// 添加全局请求拦截器，用于调试
Mock.mock(/.*/, 'get', function(options) {
  console.log('MockJS 拦截到 GET 请求:', options.url)
  return {
    code: 200,
    message: '请求成功',
    data: Mock.Random.dataImage('300x300', 'Mock Data')
  }
})

Mock.mock(/.*/, 'post', function(options) {
  console.log('MockJS 拦截到 POST 请求:', options.url)
  return {
    code: 200,
    message: '请求成功',
    data: Mock.Random.dataImage('300x300', 'Mock Data')
  }
})

console.log('MockJS 全局拦截器已启用')
